//
//  MahjongRecordView.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/30.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongRecordView.h"
#import "HWPopTool.h"

#import "MahjongRecoredTVC.h"

#import "ActionModel.h"
#import "ResultModel.h"

@interface MahjongRecordView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic,strong) NSMutableArray *resultActionArr;

@end

static NSString *kMahjongRecoredTVC = @"kMahjongRecoredTVC";

@implementation MahjongRecordView

- (void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [[HWPopTool sharedInstance]closeWithBlcok:^{}];
    }];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MahjongRecoredTVC class]) bundle:nil] forCellReuseIdentifier:kMahjongRecoredTVC];
}

- (void)detailActionResult{
    //获取与自己相关的action
    _resultActionArr = [NSMutableArray new];
    [_resultActionArr addObjectsFromArray:_gameModel.actionArr];
    for (ActionModel *am in _gameModel.actionArr) {
        BOOL flag = NO;
        if ([am.owner.playerId isEqualToString:_owner.playerId]) {
            flag = YES;
        }
        for (PlayerModel *pm in am.targetsArr) {
            if ([pm.playerId isEqualToString:_owner.playerId]) {
                flag = YES;
                break;
            }
        }
        for (PlayerModel *pm in am.caguaArr) {//擦挂
            if ([pm.playerId isEqualToString:_owner.playerId]) {
                flag = YES;
                break;
            }
        }
        if ([am.transferedTo.playerId isEqualToString:_owner.playerId]) {//转雨
            flag = YES;
        }
        if (!flag) {
            [_resultActionArr removeObject:am];
        }
    }
    //分数
    NSInteger score = 0;
    for (ResultModel *rm in _gameModel.resultArr) {
        if ([rm.playerModel.playerId isEqualToString:_owner.playerId]) {
            score = rm.score;
            break;
        }
    }

    if (score > 0) {
        _scoreLabel.textColor = UIColorHex(F15A5A);
        _scoreLabel.text = NSStringFormat(@"+%ld",score);
    }else if (score == 0){
        _scoreLabel.textColor = UIColorHex(9B9B9B);
        _scoreLabel.text = NSStringFormat(@"%ld",score);
    }else{
        _scoreLabel.textColor = UIColorHex(519B00);
        _scoreLabel.text = NSStringFormat(@"%ld",score);
    }
    
    [_tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _resultActionArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MahjongRecoredTVC *cell = [tableView dequeueReusableCellWithIdentifier:kMahjongRecoredTVC forIndexPath:indexPath];
    cell.owner = _owner;
    cell.cellIdx = indexPath.row;
    cell.gameModel = _gameModel;
    cell.resultActionArr = _resultActionArr;
    cell.actionModel = _resultActionArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 92.f;
}

+(instancetype)mahjongRecordView{
    return [[[NSBundle mainBundle] loadNibNamed:@"MahjongRecordView"
                                          owner:nil options:nil]lastObject];
}

- (IBAction)closeAction:(id)sender {
    [[HWPopTool sharedInstance]closeWithBlcok:^{}];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 取消响应手势
    if (touch.view == self){
        return YES;
    }
    return NO;
}

@end
